package com.cos.shop.web.controller;

import com.cos.shop.common.Const;
import com.cos.shop.entity.Address;
import com.cos.shop.entity.Cart;
import com.cos.shop.entity.Orders;
import com.cos.shop.entity.User;
import com.cos.shop.services.AddressServices;
import com.cos.shop.services.CartServices;
import com.cos.shop.services.OrderServices;
import com.cos.shop.services.impl.AddressServicesImpl;
import com.cos.shop.services.impl.CartServicesImpl;
import com.cos.shop.services.impl.OrderServicesImpl;
import com.cos.shop.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/2/25 10:04
 * @description:
 */
@WebServlet("/order")
public class OrderController extends BaseServlet {
    private AddressServices as = new AddressServicesImpl();
    private CartServices cs = new CartServicesImpl();

    private OrderServices os = new OrderServicesImpl();
    public String changeStatusxx(HttpServletRequest request, HttpServletResponse response) {
        //直接修改订单状态为已付款  搞不定支付宝
        return null;
    }
    public String changeStatus(HttpServletRequest request, HttpServletResponse response) {
        String oid = request.getParameter("oid");
        //访问业务层
        os.chageStatus(oid);
        //回去订单列表
        return Const.REDIRECT+"order?type=getOrderList";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/25 11:49
     * @description: 获取订单详情
     */
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) {
        String oid = request.getParameter("oid");
        //访问业务层
        Orders o = os.getOrderDeatil(oid);
        request.setAttribute("order", o);
        return Const.FORWARD+"orderDetail.jsp";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/25 11:17
     * @description: 获取订单列表
     */
    public String getOrderList(HttpServletRequest request, HttpServletResponse response) {
        //1 登录验证
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        if (u == null) {
            return Const.REDIRECT + "login.jsp";
        }
        //2 访问业务层获取用户订单数据
        List<Orders> ordersList = os.getOrderListByUid(u.getUid());
        //3 保存数据到reqeust转发
        request.setAttribute("orderList", ordersList);
        return Const.FORWARD + "orderList.jsp";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/25 10:23
     * @description: 生成订单
     */
    public String addOrder(HttpServletRequest request, HttpServletResponse response) {
        //1 登录验证
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        if (u == null) {
            return Const.REDIRECT + "login.jsp";
        }
        //2 获取用户提交的数据
        int aid = StringUtils.toInt(request.getParameter("aid"));
        //3 访问业务生成订单
        Orders orders = os.addOrder(aid, u.getUid());
        //4 判断
        if (orders != null) {
            request.setAttribute("order", orders);
            return Const.FORWARD + "orderSuccess.jsp";
        }
        request.setAttribute("msg", "下单失败,你没钱把??");
        return Const.FORWARD + "message.jsp";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/25 10:04
     * @description: 添加地址
     */
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) {
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        if (u == null) {
            return Const.REDIRECT + "login.jsp";
        }
        //获取用户所有购物车数据 uid
        List<Cart> cartList = cs.getCart(u.getUid());
        //用户所有的地址数据 uid
        List<Address> addressListByUid = as.getAddressListByUid(u.getUid());
        //存入reqeust
        request.setAttribute("cartList", cartList);
        request.setAttribute("addList", addressListByUid);
        //转发
        return Const.FORWARD + "order.jsp";
    }
}
